import React from "react";
import { Button, Checkbox, Form, Input, message } from "antd";
import { useNavigate } from "react-router-dom";
import "./index.less";
import { IResetPassword } from "@/types/login";
import { resetPassword } from "@/api/employee";
const ResetPasswordPage = () => {
  const navigate = useNavigate();
  const onFinish = async ({ oldPassword, newPassword }: IResetPassword) => {
    if (oldPassword === newPassword) {
      message.warning("新旧密码不能一样");
      return;
    }
    if (newPassword.length < 6) {
      message.warning("新长度不能低于5位");
      return;
    }
    const res = await resetPassword({ oldPassword, newPassword });
    if (res) {
      navigate("/login");
    }
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };

  //   返回
  const handleReturn = () => {
    navigate(-1);
  };

  return (
    <Form
      name="basic"
      labelCol={{ span: 8 }}
      wrapperCol={{ span: 16 }}
      style={{ maxWidth: 600 }}
      initialValues={{ remember: true }}
      onFinish={onFinish}
      onFinishFailed={onFinishFailed}
      autoComplete="off"
      className="reset-wrap"
    >
      <Form.Item
        label="旧密码"
        name="oldPassword"
        rules={[{ required: true, message: "请输入旧密码" }]}
      >
        <Input.Password placeholder="请输入旧密码" />
      </Form.Item>
      <Form.Item
        label="新密码"
        name="newPassword"
        rules={[{ required: true, message: "请输入新密码" }]}
      >
        <Input.Password placeholder="请输入新密码" />
      </Form.Item>

      <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
        <Button className="margin-bottom-16px" block onClick={handleReturn}>
          返回
        </Button>
        <Button block type="primary" htmlType="submit">
          修改
        </Button>
      </Form.Item>
    </Form>
  );
};

export default ResetPasswordPage;
